{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2 \n",
    "%matplotlib inline\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import keras.backend.tensorflow_backend as KTF\n",
    "import tensorflow as tf\n",
    "config = tf.ConfigProto()  \n",
    "config.gpu_options.allow_growth=True   \n",
    "session = tf.Session(config=config)\n",
    "KTF.set_session(session)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import keras \n",
    "from models.psenet import psenet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "shape = (None,None,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/anaconda3/envs/tf/lib/python3.5/site-packages/keras_applications/resnet50.py:265: UserWarning: The output shape of `ResNet50(include_top=False)` has been changed since Keras 2.2.0.\n",
      "  warnings.warn('The output shape of `ResNet50(include_top=False)` '\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(None, None)\n",
      "(None, None)\n",
      "(None, None)\n",
      "(None, None)\n",
      "(None, None)\n",
      "(None, None)\n",
      "(None, None)\n",
      "(None, None)\n",
      "(None, None)\n",
      "__________________________________________________________________________________________________\n",
      "Layer (type)                    Output Shape         Param #     Connected to                     \n",
      "==================================================================================================\n",
      "input_1 (InputLayer)            (None, None, None, 3 0                                            \n",
      "__________________________________________________________________________________________________\n",
      "lambda_1 (Lambda)               (None, None, None, 3 0           input_1[0][0]                    \n",
      "__________________________________________________________________________________________________\n",
      "conv1_pad (ZeroPadding2D)       (None, None, None, 3 0           lambda_1[0][0]                   \n",
      "__________________________________________________________________________________________________\n",
      "conv1 (Conv2D)                  (None, None, None, 6 9472        conv1_pad[0][0]                  \n",
      "__________________________________________________________________________________________________\n",
      "bn_conv1 (BatchNormalization)   (None, None, None, 6 256         conv1[0][0]                      \n",
      "__________________________________________________________________________________________________\n",
      "activation_1 (Activation)       (None, None, None, 6 0           bn_conv1[0][0]                   \n",
      "__________________________________________________________________________________________________\n",
      "pool1_pad (ZeroPadding2D)       (None, None, None, 6 0           activation_1[0][0]               \n",
      "__________________________________________________________________________________________________\n",
      "max_pooling2d_1 (MaxPooling2D)  (None, None, None, 6 0           pool1_pad[0][0]                  \n",
      "__________________________________________________________________________________________________\n",
      "res2a_branch2a (Conv2D)         (None, None, None, 6 4160        max_pooling2d_1[0][0]            \n",
      "__________________________________________________________________________________________________\n",
      "bn2a_branch2a (BatchNormalizati (None, None, None, 6 256         res2a_branch2a[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "activation_2 (Activation)       (None, None, None, 6 0           bn2a_branch2a[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "res2a_branch2b (Conv2D)         (None, None, None, 6 36928       activation_2[0][0]               \n",
      "__________________________________________________________________________________________________\n",
      "bn2a_branch2b (BatchNormalizati (None, None, None, 6 256         res2a_branch2b[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "activation_3 (Activation)       (None, None, None, 6 0           bn2a_branch2b[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "res2a_branch2c (Conv2D)         (None, None, None, 2 16640       activation_3[0][0]               \n",
      "__________________________________________________________________________________________________\n",
      "res2a_branch1 (Conv2D)          (None, None, None, 2 16640       max_pooling2d_1[0][0]            \n",
      "__________________________________________________________________________________________________\n",
      "bn2a_branch2c (BatchNormalizati (None, None, None, 2 1024        res2a_branch2c[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "bn2a_branch1 (BatchNormalizatio (None, None, None, 2 1024        res2a_branch1[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "add_1 (Add)                     (None, None, None, 2 0           bn2a_branch2c[0][0]              \n",
      "                                                                 bn2a_branch1[0][0]               \n",
      "__________________________________________________________________________________________________\n",
      "activation_4 (Activation)       (None, None, None, 2 0           add_1[0][0]                      \n",
      "__________________________________________________________________________________________________\n",
      "res2b_branch2a (Conv2D)         (None, None, None, 6 16448       activation_4[0][0]               \n",
      "__________________________________________________________________________________________________\n",
      "bn2b_branch2a (BatchNormalizati (None, None, None, 6 256         res2b_branch2a[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "activation_5 (Activation)       (None, None, None, 6 0           bn2b_branch2a[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "res2b_branch2b (Conv2D)         (None, None, None, 6 36928       activation_5[0][0]               \n",
      "__________________________________________________________________________________________________\n",
      "bn2b_branch2b (BatchNormalizati (None, None, None, 6 256         res2b_branch2b[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "activation_6 (Activation)       (None, None, None, 6 0           bn2b_branch2b[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "res2b_branch2c (Conv2D)         (None, None, None, 2 16640       activation_6[0][0]               \n",
      "__________________________________________________________________________________________________\n",
      "bn2b_branch2c (BatchNormalizati (None, None, None, 2 1024        res2b_branch2c[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "add_2 (Add)                     (None, None, None, 2 0           bn2b_branch2c[0][0]              \n",
      "                                                                 activation_4[0][0]               \n",
      "__________________________________________________________________________________________________\n",
      "activation_7 (Activation)       (None, None, None, 2 0           add_2[0][0]                      \n",
      "__________________________________________________________________________________________________\n",
      "res2c_branch2a (Conv2D)         (None, None, None, 6 16448       activation_7[0][0]               \n",
      "__________________________________________________________________________________________________\n",
      "bn2c_branch2a (BatchNormalizati (None, None, None, 6 256         res2c_branch2a[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "activation_8 (Activation)       (None, None, None, 6 0           bn2c_branch2a[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "res2c_branch2b (Conv2D)         (None, None, None, 6 36928       activation_8[0][0]               \n",
      "__________________________________________________________________________________________________\n",
      "bn2c_branch2b (BatchNormalizati (None, None, None, 6 256         res2c_branch2b[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "activation_9 (Activation)       (None, None, None, 6 0           bn2c_branch2b[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "res2c_branch2c (Conv2D)         (None, None, None, 2 16640       activation_9[0][0]               \n",
      "__________________________________________________________________________________________________\n",
      "bn2c_branch2c (BatchNormalizati (None, None, None, 2 1024        res2c_branch2c[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "add_3 (Add)                     (None, None, None, 2 0           bn2c_branch2c[0][0]              \n",
      "                                                                 activation_7[0][0]               \n",
      "__________________________________________________________________________________________________\n",
      "activation_10 (Activation)      (None, None, None, 2 0           add_3[0][0]                      \n",
      "__________________________________________________________________________________________________\n",
      "res3a_branch2a (Conv2D)         (None, None, None, 1 32896       activation_10[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "bn3a_branch2a (BatchNormalizati (None, None, None, 1 512         res3a_branch2a[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "activation_11 (Activation)      (None, None, None, 1 0           bn3a_branch2a[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "res3a_branch2b (Conv2D)         (None, None, None, 1 147584      activation_11[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "bn3a_branch2b (BatchNormalizati (None, None, None, 1 512         res3a_branch2b[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "activation_12 (Activation)      (None, None, None, 1 0           bn3a_branch2b[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "res3a_branch2c (Conv2D)         (None, None, None, 5 66048       activation_12[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "res3a_branch1 (Conv2D)          (None, None, None, 5 131584      activation_10[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "bn3a_branch2c (BatchNormalizati (None, None, None, 5 2048        res3a_branch2c[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "bn3a_branch1 (BatchNormalizatio (None, None, None, 5 2048        res3a_branch1[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "add_4 (Add)                     (None, None, None, 5 0           bn3a_branch2c[0][0]              \n",
      "                                                                 bn3a_branch1[0][0]               \n",
      "__________________________________________________________________________________________________\n",
      "activation_13 (Activation)      (None, None, None, 5 0           add_4[0][0]                      \n",
      "__________________________________________________________________________________________________\n",
      "res3b_branch2a (Conv2D)         (None, None, None, 1 65664       activation_13[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "bn3b_branch2a (BatchNormalizati (None, None, None, 1 512         res3b_branch2a[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "activation_14 (Activation)      (None, None, None, 1 0           bn3b_branch2a[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "res3b_branch2b (Conv2D)         (None, None, None, 1 147584      activation_14[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "bn3b_branch2b (BatchNormalizati (None, None, None, 1 512         res3b_branch2b[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "activation_15 (Activation)      (None, None, None, 1 0           bn3b_branch2b[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "res3b_branch2c (Conv2D)         (None, None, None, 5 66048       activation_15[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "bn3b_branch2c (BatchNormalizati (None, None, None, 5 2048        res3b_branch2c[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "add_5 (Add)                     (None, None, None, 5 0           bn3b_branch2c[0][0]              \n",
      "                                                                 activation_13[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "activation_16 (Activation)      (None, None, None, 5 0           add_5[0][0]                      \n",
      "__________________________________________________________________________________________________\n",
      "res3c_branch2a (Conv2D)         (None, None, None, 1 65664       activation_16[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "bn3c_branch2a (BatchNormalizati (None, None, None, 1 512         res3c_branch2a[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "activation_17 (Activation)      (None, None, None, 1 0           bn3c_branch2a[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "res3c_branch2b (Conv2D)         (None, None, None, 1 147584      activation_17[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "bn3c_branch2b (BatchNormalizati (None, None, None, 1 512         res3c_branch2b[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "activation_18 (Activation)      (None, None, None, 1 0           bn3c_branch2b[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "res3c_branch2c (Conv2D)         (None, None, None, 5 66048       activation_18[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "bn3c_branch2c (BatchNormalizati (None, None, None, 5 2048        res3c_branch2c[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "add_6 (Add)                     (None, None, None, 5 0           bn3c_branch2c[0][0]              \n",
      "                                                                 activation_16[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "activation_19 (Activation)      (None, None, None, 5 0           add_6[0][0]                      \n",
      "__________________________________________________________________________________________________\n",
      "res3d_branch2a (Conv2D)         (None, None, None, 1 65664       activation_19[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "bn3d_branch2a (BatchNormalizati (None, None, None, 1 512         res3d_branch2a[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "activation_20 (Activation)      (None, None, None, 1 0           bn3d_branch2a[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "res3d_branch2b (Conv2D)         (None, None, None, 1 147584      activation_20[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "bn3d_branch2b (BatchNormalizati (None, None, None, 1 512         res3d_branch2b[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "activation_21 (Activation)      (None, None, None, 1 0           bn3d_branch2b[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "res3d_branch2c (Conv2D)         (None, None, None, 5 66048       activation_21[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "bn3d_branch2c (BatchNormalizati (None, None, None, 5 2048        res3d_branch2c[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "add_7 (Add)                     (None, None, None, 5 0           bn3d_branch2c[0][0]              \n",
      "                                                                 activation_19[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "activation_22 (Activation)      (None, None, None, 5 0           add_7[0][0]                      \n",
      "__________________________________________________________________________________________________\n",
      "res4a_branch2a (Conv2D)         (None, None, None, 2 131328      activation_22[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "bn4a_branch2a (BatchNormalizati (None, None, None, 2 1024        res4a_branch2a[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "activation_23 (Activation)      (None, None, None, 2 0           bn4a_branch2a[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "res4a_branch2b (Conv2D)         (None, None, None, 2 590080      activation_23[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "bn4a_branch2b (BatchNormalizati (None, None, None, 2 1024        res4a_branch2b[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "activation_24 (Activation)      (None, None, None, 2 0           bn4a_branch2b[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "res4a_branch2c (Conv2D)         (None, None, None, 1 263168      activation_24[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "res4a_branch1 (Conv2D)          (None, None, None, 1 525312      activation_22[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "bn4a_branch2c (BatchNormalizati (None, None, None, 1 4096        res4a_branch2c[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "bn4a_branch1 (BatchNormalizatio (None, None, None, 1 4096        res4a_branch1[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "add_8 (Add)                     (None, None, None, 1 0           bn4a_branch2c[0][0]              \n",
      "                                                                 bn4a_branch1[0][0]               \n",
      "__________________________________________________________________________________________________\n",
      "activation_25 (Activation)      (None, None, None, 1 0           add_8[0][0]                      \n",
      "__________________________________________________________________________________________________\n",
      "res4b_branch2a (Conv2D)         (None, None, None, 2 262400      activation_25[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "bn4b_branch2a (BatchNormalizati (None, None, None, 2 1024        res4b_branch2a[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "activation_26 (Activation)      (None, None, None, 2 0           bn4b_branch2a[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "res4b_branch2b (Conv2D)         (None, None, None, 2 590080      activation_26[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "bn4b_branch2b (BatchNormalizati (None, None, None, 2 1024        res4b_branch2b[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "activation_27 (Activation)      (None, None, None, 2 0           bn4b_branch2b[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "res4b_branch2c (Conv2D)         (None, None, None, 1 263168      activation_27[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "bn4b_branch2c (BatchNormalizati (None, None, None, 1 4096        res4b_branch2c[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "add_9 (Add)                     (None, None, None, 1 0           bn4b_branch2c[0][0]              \n",
      "                                                                 activation_25[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "activation_28 (Activation)      (None, None, None, 1 0           add_9[0][0]                      \n",
      "__________________________________________________________________________________________________\n",
      "res4c_branch2a (Conv2D)         (None, None, None, 2 262400      activation_28[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "bn4c_branch2a (BatchNormalizati (None, None, None, 2 1024        res4c_branch2a[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "activation_29 (Activation)      (None, None, None, 2 0           bn4c_branch2a[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "res4c_branch2b (Conv2D)         (None, None, None, 2 590080      activation_29[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "bn4c_branch2b (BatchNormalizati (None, None, None, 2 1024        res4c_branch2b[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "activation_30 (Activation)      (None, None, None, 2 0           bn4c_branch2b[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "res4c_branch2c (Conv2D)         (None, None, None, 1 263168      activation_30[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "bn4c_branch2c (BatchNormalizati (None, None, None, 1 4096        res4c_branch2c[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "add_10 (Add)                    (None, None, None, 1 0           bn4c_branch2c[0][0]              \n",
      "                                                                 activation_28[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "activation_31 (Activation)      (None, None, None, 1 0           add_10[0][0]                     \n",
      "__________________________________________________________________________________________________\n",
      "res4d_branch2a (Conv2D)         (None, None, None, 2 262400      activation_31[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "bn4d_branch2a (BatchNormalizati (None, None, None, 2 1024        res4d_branch2a[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "activation_32 (Activation)      (None, None, None, 2 0           bn4d_branch2a[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "res4d_branch2b (Conv2D)         (None, None, None, 2 590080      activation_32[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "bn4d_branch2b (BatchNormalizati (None, None, None, 2 1024        res4d_branch2b[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "activation_33 (Activation)      (None, None, None, 2 0           bn4d_branch2b[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "res4d_branch2c (Conv2D)         (None, None, None, 1 263168      activation_33[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "bn4d_branch2c (BatchNormalizati (None, None, None, 1 4096        res4d_branch2c[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "add_11 (Add)                    (None, None, None, 1 0           bn4d_branch2c[0][0]              \n",
      "                                                                 activation_31[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "activation_34 (Activation)      (None, None, None, 1 0           add_11[0][0]                     \n",
      "__________________________________________________________________________________________________\n",
      "res4e_branch2a (Conv2D)         (None, None, None, 2 262400      activation_34[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "bn4e_branch2a (BatchNormalizati (None, None, None, 2 1024        res4e_branch2a[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "activation_35 (Activation)      (None, None, None, 2 0           bn4e_branch2a[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "res4e_branch2b (Conv2D)         (None, None, None, 2 590080      activation_35[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "bn4e_branch2b (BatchNormalizati (None, None, None, 2 1024        res4e_branch2b[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "activation_36 (Activation)      (None, None, None, 2 0           bn4e_branch2b[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "res4e_branch2c (Conv2D)         (None, None, None, 1 263168      activation_36[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "bn4e_branch2c (BatchNormalizati (None, None, None, 1 4096        res4e_branch2c[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "add_12 (Add)                    (None, None, None, 1 0           bn4e_branch2c[0][0]              \n",
      "                                                                 activation_34[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "activation_37 (Activation)      (None, None, None, 1 0           add_12[0][0]                     \n",
      "__________________________________________________________________________________________________\n",
      "res4f_branch2a (Conv2D)         (None, None, None, 2 262400      activation_37[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "bn4f_branch2a (BatchNormalizati (None, None, None, 2 1024        res4f_branch2a[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "activation_38 (Activation)      (None, None, None, 2 0           bn4f_branch2a[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "res4f_branch2b (Conv2D)         (None, None, None, 2 590080      activation_38[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "bn4f_branch2b (BatchNormalizati (None, None, None, 2 1024        res4f_branch2b[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "activation_39 (Activation)      (None, None, None, 2 0           bn4f_branch2b[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "res4f_branch2c (Conv2D)         (None, None, None, 1 263168      activation_39[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "bn4f_branch2c (BatchNormalizati (None, None, None, 1 4096        res4f_branch2c[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "add_13 (Add)                    (None, None, None, 1 0           bn4f_branch2c[0][0]              \n",
      "                                                                 activation_37[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "activation_40 (Activation)      (None, None, None, 1 0           add_13[0][0]                     \n",
      "__________________________________________________________________________________________________\n",
      "res5a_branch2a (Conv2D)         (None, None, None, 5 524800      activation_40[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "bn5a_branch2a (BatchNormalizati (None, None, None, 5 2048        res5a_branch2a[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "activation_41 (Activation)      (None, None, None, 5 0           bn5a_branch2a[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "res5a_branch2b (Conv2D)         (None, None, None, 5 2359808     activation_41[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "bn5a_branch2b (BatchNormalizati (None, None, None, 5 2048        res5a_branch2b[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "activation_42 (Activation)      (None, None, None, 5 0           bn5a_branch2b[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "res5a_branch2c (Conv2D)         (None, None, None, 2 1050624     activation_42[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "res5a_branch1 (Conv2D)          (None, None, None, 2 2099200     activation_40[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "bn5a_branch2c (BatchNormalizati (None, None, None, 2 8192        res5a_branch2c[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "bn5a_branch1 (BatchNormalizatio (None, None, None, 2 8192        res5a_branch1[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "add_14 (Add)                    (None, None, None, 2 0           bn5a_branch2c[0][0]              \n",
      "                                                                 bn5a_branch1[0][0]               \n",
      "__________________________________________________________________________________________________\n",
      "activation_43 (Activation)      (None, None, None, 2 0           add_14[0][0]                     \n",
      "__________________________________________________________________________________________________\n",
      "res5b_branch2a (Conv2D)         (None, None, None, 5 1049088     activation_43[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "bn5b_branch2a (BatchNormalizati (None, None, None, 5 2048        res5b_branch2a[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "activation_44 (Activation)      (None, None, None, 5 0           bn5b_branch2a[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "res5b_branch2b (Conv2D)         (None, None, None, 5 2359808     activation_44[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "bn5b_branch2b (BatchNormalizati (None, None, None, 5 2048        res5b_branch2b[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "activation_45 (Activation)      (None, None, None, 5 0           bn5b_branch2b[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "res5b_branch2c (Conv2D)         (None, None, None, 2 1050624     activation_45[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "bn5b_branch2c (BatchNormalizati (None, None, None, 2 8192        res5b_branch2c[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "add_15 (Add)                    (None, None, None, 2 0           bn5b_branch2c[0][0]              \n",
      "                                                                 activation_43[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "activation_46 (Activation)      (None, None, None, 2 0           add_15[0][0]                     \n",
      "__________________________________________________________________________________________________\n",
      "res5c_branch2a (Conv2D)         (None, None, None, 5 1049088     activation_46[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "bn5c_branch2a (BatchNormalizati (None, None, None, 5 2048        res5c_branch2a[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "activation_47 (Activation)      (None, None, None, 5 0           bn5c_branch2a[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "res5c_branch2b (Conv2D)         (None, None, None, 5 2359808     activation_47[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "bn5c_branch2b (BatchNormalizati (None, None, None, 5 2048        res5c_branch2b[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "activation_48 (Activation)      (None, None, None, 5 0           bn5c_branch2b[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "res5c_branch2c (Conv2D)         (None, None, None, 2 1050624     activation_48[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "bn5c_branch2c (BatchNormalizati (None, None, None, 2 8192        res5c_branch2c[0][0]             \n",
      "__________________________________________________________________________________________________\n",
      "add_16 (Add)                    (None, None, None, 2 0           bn5c_branch2c[0][0]              \n",
      "                                                                 activation_46[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "activation_49 (Activation)      (None, None, None, 2 0           add_16[0][0]                     \n",
      "__________________________________________________________________________________________________\n",
      "conv2d_1 (Conv2D)               (None, None, None, 2 524544      activation_49[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "resize_image_1 (resize_image)   (None, None, None, 2 0           conv2d_1[0][0]                   \n",
      "__________________________________________________________________________________________________\n",
      "conv2d_2 (Conv2D)               (None, None, None, 2 262400      activation_37[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "concatenate_1 (Concatenate)     (None, None, None, 5 0           resize_image_1[0][0]             \n",
      "                                                                 conv2d_2[0][0]                   \n",
      "__________________________________________________________________________________________________\n",
      "conv2d_4 (Conv2D)               (None, None, None, 1 589952      concatenate_1[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "batch_normalization_1 (BatchNor (None, None, None, 1 512         conv2d_4[0][0]                   \n",
      "__________________________________________________________________________________________________\n",
      "activation_50 (Activation)      (None, None, None, 1 0           batch_normalization_1[0][0]      \n",
      "__________________________________________________________________________________________________\n",
      "resize_image_2 (resize_image)   (None, None, None, 1 0           activation_50[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "conv2d_3 (Conv2D)               (None, None, None, 2 131328      activation_19[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "concatenate_2 (Concatenate)     (None, None, None, 3 0           resize_image_2[0][0]             \n",
      "                                                                 conv2d_3[0][0]                   \n",
      "__________________________________________________________________________________________________\n",
      "conv2d_5 (Conv2D)               (None, None, None, 1 442496      concatenate_2[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "batch_normalization_2 (BatchNor (None, None, None, 1 512         conv2d_5[0][0]                   \n",
      "__________________________________________________________________________________________________\n",
      "activation_51 (Activation)      (None, None, None, 1 0           batch_normalization_2[0][0]      \n",
      "__________________________________________________________________________________________________\n",
      "resize_image_3 (resize_image)   (None, None, None, 1 0           activation_51[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "concatenate_3 (Concatenate)     (None, None, None, 3 0           resize_image_3[0][0]             \n",
      "                                                                 activation_7[0][0]               \n",
      "__________________________________________________________________________________________________\n",
      "conv2d_6 (Conv2D)               (None, None, None, 1 442496      concatenate_3[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "batch_normalization_3 (BatchNor (None, None, None, 1 512         conv2d_6[0][0]                   \n",
      "__________________________________________________________________________________________________\n",
      "activation_52 (Activation)      (None, None, None, 1 0           batch_normalization_3[0][0]      \n",
      "__________________________________________________________________________________________________\n",
      "resize_image_4 (resize_image)   (None, None, None, 1 0           activation_52[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "concatenate_4 (Concatenate)     (None, None, None, 1 0           resize_image_4[0][0]             \n",
      "                                                                 activation_1[0][0]               \n",
      "__________________________________________________________________________________________________\n",
      "conv2d_7 (Conv2D)               (None, None, None, 1 221312      concatenate_4[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "batch_normalization_4 (BatchNor (None, None, None, 1 512         conv2d_7[0][0]                   \n",
      "__________________________________________________________________________________________________\n",
      "resize_image_5 (resize_image)   (None, None, None, 2 0           conv2d_1[0][0]                   \n",
      "__________________________________________________________________________________________________\n",
      "resize_image_6 (resize_image)   (None, None, None, 1 0           activation_50[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "resize_image_7 (resize_image)   (None, None, None, 1 0           activation_51[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "resize_image_8 (resize_image)   (None, None, None, 1 0           activation_52[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "activation_53 (Activation)      (None, None, None, 1 0           batch_normalization_4[0][0]      \n",
      "__________________________________________________________________________________________________\n",
      "concatenate_5 (Concatenate)     (None, None, None, 7 0           resize_image_5[0][0]             \n",
      "                                                                 resize_image_6[0][0]             \n",
      "                                                                 resize_image_7[0][0]             \n",
      "                                                                 resize_image_8[0][0]             \n",
      "                                                                 activation_53[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "conv2d_8 (Conv2D)               (None, None, None, 2 1769728     concatenate_5[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "batch_normalization_5 (BatchNor (None, None, None, 2 1024        conv2d_8[0][0]                   \n",
      "__________________________________________________________________________________________________\n",
      "activation_54 (Activation)      (None, None, None, 2 0           batch_normalization_5[0][0]      \n",
      "__________________________________________________________________________________________________\n",
      "conv2d_9 (Conv2D)               (None, None, None, 6 1542        activation_54[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "resize_image_9 (resize_image)   (None, None, None, 6 0           conv2d_9[0][0]                   \n",
      "__________________________________________________________________________________________________\n",
      "activation_55 (Activation)      (None, None, None, 6 0           resize_image_9[0][0]             \n",
      "==================================================================================================\n",
      "Total params: 27,976,582\n",
      "Trainable params: 27,921,926\n",
      "Non-trainable params: 54,656\n",
      "__________________________________________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "inputs = keras.layers.Input(shape=shape)\n",
    "output = psenet(inputs)\n",
    "model  = keras.models.Model(inputs,output)\n",
    "model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "from keras.optimizers import Adam\n",
    "from models.loss import build_loss\n",
    "from models.metrics import build_iou,mean_iou\n",
    "from keras.utils import multi_gpu_model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(None, None)\n",
      "(None, None)\n",
      "(None, None)\n",
      "(None, None)\n",
      "(None, None)\n",
      "(None, None)\n",
      "(None, None)\n",
      "(None, None)\n",
      "(None, None)\n",
      "(None, None)\n",
      "(None, None)\n",
      "(None, None)\n",
      "(None, None)\n",
      "(None, None)\n",
      "(None, None)\n",
      "(None, None)\n",
      "(None, None)\n",
      "(None, None)\n"
     ]
    }
   ],
   "source": [
    "parallel_model = multi_gpu_model(model)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "adam = Adam(1e-4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "ious = build_iou([0,1],['bk','txt'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "parallel_model.compile(loss=build_loss,\n",
    "              optimizer=adam,\n",
    "              metrics=ious)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "import config \n",
    "from tool.generator import Generator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_dir = config.MIWI_2018_TRAIN_LABEL_DIR\n",
    "test_dir = config.MIWI_2018_TEST_LABEL_DIR\n",
    "batch_size = 4\n",
    "num_class =2 \n",
    "shape = (640,640)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "gen_train = Generator(train_dir,batch_size = batch_size ,istraining=True,num_classes=num_class,mirror = False,reshape=shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "gen_test = Generator(test_dir,batch_size = batch_size ,istraining=False,num_classes=num_class,\n",
    "                     reshape=shape,mirror=False,scale=False,clip=False,trans_color=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "from keras.callbacks import ModelCheckpoint\n",
    "from keras.callbacks import TensorBoard\n",
    "checkpoint = ModelCheckpoint(r'resent50-190219_BLINEAR-{epoch:02d}.hdf5',\n",
    "                           save_weights_only=True)\n",
    "tb = TensorBoard(log_dir='./logs', update_freq=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/40\n",
      "2255/2255 [==============================] - 2021s 896ms/step - loss: 0.2130 - iou_bk: 0.9275 - iou_txt: 0.6898 - val_loss: 0.1418 - val_iou_bk: 0.9499 - val_iou_txt: 0.7825\n",
      "Epoch 2/40\n",
      "2255/2255 [==============================] - 1998s 886ms/step - loss: 0.1731 - iou_bk: 0.9404 - iou_txt: 0.7373 - val_loss: 0.1311 - val_iou_bk: 0.9544 - val_iou_txt: 0.7960\n",
      "Epoch 3/40\n",
      "2255/2255 [==============================] - 2004s 889ms/step - loss: 0.1666 - iou_bk: 0.9429 - iou_txt: 0.7453 - val_loss: 0.1379 - val_iou_bk: 0.9509 - val_iou_txt: 0.7877\n",
      "Epoch 4/40\n",
      "2255/2255 [==============================] - 1988s 881ms/step - loss: 0.1573 - iou_bk: 0.9458 - iou_txt: 0.7576 - val_loss: 0.1264 - val_iou_bk: 0.9548 - val_iou_txt: 0.7924\n",
      "Epoch 5/40\n",
      "2255/2255 [==============================] - 1998s 886ms/step - loss: 0.1532 - iou_bk: 0.9470 - iou_txt: 0.7641 - val_loss: 0.1187 - val_iou_bk: 0.9585 - val_iou_txt: 0.8127\n",
      "Epoch 6/40\n",
      "2255/2255 [==============================] - 1992s 884ms/step - loss: 0.1510 - iou_bk: 0.9489 - iou_txt: 0.7667 - val_loss: 0.1255 - val_iou_bk: 0.9554 - val_iou_txt: 0.8039\n",
      "Epoch 7/40\n",
      "2255/2255 [==============================] - 1998s 886ms/step - loss: 0.1464 - iou_bk: 0.9491 - iou_txt: 0.7734 - val_loss: 0.1253 - val_iou_bk: 0.9547 - val_iou_txt: 0.8037\n",
      "Epoch 8/40\n",
      "2255/2255 [==============================] - 1998s 886ms/step - loss: 0.1430 - iou_bk: 0.9506 - iou_txt: 0.7781 - val_loss: 0.1200 - val_iou_bk: 0.9570 - val_iou_txt: 0.8100\n",
      "Epoch 9/40\n",
      "2255/2255 [==============================] - 1991s 883ms/step - loss: 0.1411 - iou_bk: 0.9511 - iou_txt: 0.7813 - val_loss: 0.1145 - val_iou_bk: 0.9597 - val_iou_txt: 0.8162\n",
      "Epoch 10/40\n",
      "2255/2255 [==============================] - 2001s 888ms/step - loss: 0.1417 - iou_bk: 0.9511 - iou_txt: 0.7805 - val_loss: 0.1092 - val_iou_bk: 0.9615 - val_iou_txt: 0.8230\n",
      "Epoch 11/40\n",
      "2255/2255 [==============================] - 2003s 888ms/step - loss: 0.1390 - iou_bk: 0.9515 - iou_txt: 0.7839 - val_loss: 0.1116 - val_iou_bk: 0.9605 - val_iou_txt: 0.8193\n",
      "Epoch 12/40\n",
      "2255/2255 [==============================] - 2001s 887ms/step - loss: 0.1392 - iou_bk: 0.9526 - iou_txt: 0.7834 - val_loss: 0.1118 - val_iou_bk: 0.9608 - val_iou_txt: 0.8213\n",
      "Epoch 13/40\n",
      "2255/2255 [==============================] - 2009s 891ms/step - loss: 0.1385 - iou_bk: 0.9527 - iou_txt: 0.7847 - val_loss: 0.1136 - val_iou_bk: 0.9600 - val_iou_txt: 0.8191\n",
      "Epoch 14/40\n",
      "2255/2255 [==============================] - 2003s 888ms/step - loss: 0.1373 - iou_bk: 0.9527 - iou_txt: 0.7863 - val_loss: 0.1151 - val_iou_bk: 0.9584 - val_iou_txt: 0.8147\n",
      "Epoch 15/40\n",
      "2255/2255 [==============================] - 1995s 885ms/step - loss: 0.1342 - iou_bk: 0.9540 - iou_txt: 0.7919 - val_loss: 0.1132 - val_iou_bk: 0.9617 - val_iou_txt: 0.8230\n",
      "Epoch 16/40\n",
      "2255/2255 [==============================] - 1994s 884ms/step - loss: 0.1347 - iou_bk: 0.9550 - iou_txt: 0.7910 - val_loss: 0.1182 - val_iou_bk: 0.9592 - val_iou_txt: 0.8082\n",
      "Epoch 17/40\n",
      "2255/2255 [==============================] - 1993s 884ms/step - loss: 0.1328 - iou_bk: 0.9545 - iou_txt: 0.7923 - val_loss: 0.1100 - val_iou_bk: 0.9611 - val_iou_txt: 0.8257\n",
      "Epoch 18/40\n",
      "2255/2255 [==============================] - 1993s 884ms/step - loss: 0.1293 - iou_bk: 0.9558 - iou_txt: 0.7982 - val_loss: 0.1053 - val_iou_bk: 0.9637 - val_iou_txt: 0.8307\n",
      "Epoch 19/40\n",
      "2255/2255 [==============================] - 1994s 884ms/step - loss: 0.1299 - iou_bk: 0.9555 - iou_txt: 0.7978 - val_loss: 0.1075 - val_iou_bk: 0.9629 - val_iou_txt: 0.8303\n",
      "Epoch 20/40\n",
      "2255/2255 [==============================] - 2000s 887ms/step - loss: 0.1285 - iou_bk: 0.9554 - iou_txt: 0.7994 - val_loss: 0.1049 - val_iou_bk: 0.9636 - val_iou_txt: 0.8325\n",
      "Epoch 21/40\n",
      "2255/2255 [==============================] - 1994s 884ms/step - loss: 0.1282 - iou_bk: 0.9556 - iou_txt: 0.7995 - val_loss: 0.1018 - val_iou_bk: 0.9645 - val_iou_txt: 0.8341\n",
      "Epoch 22/40\n",
      "2255/2255 [==============================] - 1997s 885ms/step - loss: 0.1276 - iou_bk: 0.9568 - iou_txt: 0.8011 - val_loss: 0.1036 - val_iou_bk: 0.9632 - val_iou_txt: 0.8336\n",
      "Epoch 23/40\n",
      "2255/2255 [==============================] - 2002s 888ms/step - loss: 0.1271 - iou_bk: 0.9566 - iou_txt: 0.8023 - val_loss: 0.1082 - val_iou_bk: 0.9617 - val_iou_txt: 0.8256\n",
      "Epoch 24/40\n",
      "2255/2255 [==============================] - 1998s 886ms/step - loss: 0.1267 - iou_bk: 0.9566 - iou_txt: 0.8018 - val_loss: 0.1075 - val_iou_bk: 0.9622 - val_iou_txt: 0.8281\n",
      "Epoch 25/40\n",
      "2255/2255 [==============================] - 1998s 886ms/step - loss: 0.1261 - iou_bk: 0.9573 - iou_txt: 0.8034 - val_loss: 0.1103 - val_iou_bk: 0.9613 - val_iou_txt: 0.8261\n",
      "Epoch 26/40\n",
      "2255/2255 [==============================] - 2008s 890ms/step - loss: 0.1263 - iou_bk: 0.9570 - iou_txt: 0.8031 - val_loss: 0.1018 - val_iou_bk: 0.9642 - val_iou_txt: 0.8342\n",
      "Epoch 27/40\n",
      "2255/2255 [==============================] - 1999s 887ms/step - loss: 0.1240 - iou_bk: 0.9574 - iou_txt: 0.8062 - val_loss: 0.1023 - val_iou_bk: 0.9640 - val_iou_txt: 0.8346\n",
      "Epoch 28/40\n",
      "2255/2255 [==============================] - 1990s 882ms/step - loss: 0.1277 - iou_bk: 0.9556 - iou_txt: 0.8008 - val_loss: 0.1041 - val_iou_bk: 0.9636 - val_iou_txt: 0.8320\n",
      "Epoch 29/40\n",
      "2255/2255 [==============================] - 1997s 886ms/step - loss: 0.1248 - iou_bk: 0.9572 - iou_txt: 0.8050 - val_loss: 0.1031 - val_iou_bk: 0.9639 - val_iou_txt: 0.8328\n",
      "Epoch 30/40\n",
      "2255/2255 [==============================] - 1994s 884ms/step - loss: 0.1230 - iou_bk: 0.9580 - iou_txt: 0.8078 - val_loss: 0.1020 - val_iou_bk: 0.9642 - val_iou_txt: 0.8353\n",
      "Epoch 31/40\n",
      "2255/2255 [==============================] - 1998s 886ms/step - loss: 0.1246 - iou_bk: 0.9575 - iou_txt: 0.8051 - val_loss: 0.1018 - val_iou_bk: 0.9642 - val_iou_txt: 0.8347\n",
      "Epoch 32/40\n",
      "2255/2255 [==============================] - 1998s 886ms/step - loss: 0.1209 - iou_bk: 0.9594 - iou_txt: 0.8102 - val_loss: 0.1008 - val_iou_bk: 0.9646 - val_iou_txt: 0.8360\n",
      "Epoch 33/40\n",
      "2255/2255 [==============================] - 1986s 881ms/step - loss: 0.1198 - iou_bk: 0.9587 - iou_txt: 0.8116 - val_loss: 0.1052 - val_iou_bk: 0.9625 - val_iou_txt: 0.8327\n",
      "Epoch 34/40\n",
      "2255/2255 [==============================] - 1989s 882ms/step - loss: 0.1211 - iou_bk: 0.9583 - iou_txt: 0.8103 - val_loss: 0.1013 - val_iou_bk: 0.9649 - val_iou_txt: 0.8346\n",
      "Epoch 35/40\n",
      "2255/2255 [==============================] - 1997s 886ms/step - loss: 0.1206 - iou_bk: 0.9586 - iou_txt: 0.8109 - val_loss: 0.1011 - val_iou_bk: 0.9639 - val_iou_txt: 0.8367\n",
      "Epoch 36/40\n",
      "2255/2255 [==============================] - 1999s 887ms/step - loss: 0.1207 - iou_bk: 0.9589 - iou_txt: 0.8108 - val_loss: 0.1009 - val_iou_bk: 0.9647 - val_iou_txt: 0.8381\n",
      "Epoch 37/40\n",
      "2255/2255 [==============================] - 1988s 881ms/step - loss: 0.1191 - iou_bk: 0.9596 - iou_txt: 0.8137 - val_loss: 0.1008 - val_iou_bk: 0.9646 - val_iou_txt: 0.8367\n",
      "Epoch 38/40\n",
      "2255/2255 [==============================] - 1986s 881ms/step - loss: 0.1189 - iou_bk: 0.9592 - iou_txt: 0.8133 - val_loss: 0.1028 - val_iou_bk: 0.9638 - val_iou_txt: 0.8345\n",
      "Epoch 39/40\n",
      "2255/2255 [==============================] - 2000s 887ms/step - loss: 0.1197 - iou_bk: 0.9592 - iou_txt: 0.8122 - val_loss: 0.1022 - val_iou_bk: 0.9647 - val_iou_txt: 0.8357\n",
      "Epoch 40/40\n",
      "2255/2255 [==============================] - 1986s 881ms/step - loss: 0.1204 - iou_bk: 0.9590 - iou_txt: 0.8117 - val_loss: 0.1013 - val_iou_bk: 0.9644 - val_iou_txt: 0.8319\n"
     ]
    }
   ],
   "source": [
    "res = parallel_model.fit_generator(gen_train,\n",
    "                          steps_per_epoch =gen_train.num_samples()// batch_size,\n",
    "                          epochs = 40,\n",
    "                          validation_data=gen_test,\n",
    "                          validation_steps =gen_test.num_samples()//batch_size,\n",
    "                          verbose=1,\n",
    "                          initial_epoch=0,\n",
    "                          workers=4,\n",
    "                          max_queue_size=16,\n",
    "                          callbacks=[checkpoint,tb])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "parallel_model.optimizer.lr = 1e-5\n",
    "res = parallel_model.fit_generator(gen_train,\n",
    "                          steps_per_epoch =gen_train.num_samples()// batch_size,\n",
    "                          epochs = 70,\n",
    "                          validation_data=gen_test,\n",
    "                          validation_steps =gen_test.num_samples()//batch_size,\n",
    "                          verbose=1,\n",
    "                          initial_epoch=37,\n",
    "                          workers=4,\n",
    "                          max_queue_size=16,\n",
    "                          callbacks=[checkpoint,tb])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 71/150\n",
      "2255/2255 [==============================] - 2020s 896ms/step - loss: 0.0888 - iou_bk: 0.9682 - iou_txt: 0.8549 - val_loss: 0.0912 - val_iou_bk: 0.9673 - val_iou_txt: 0.8504\n",
      "Epoch 72/150\n",
      "2255/2255 [==============================] - 1989s 882ms/step - loss: 0.0878 - iou_bk: 0.9688 - iou_txt: 0.8565 - val_loss: 0.0895 - val_iou_bk: 0.9682 - val_iou_txt: 0.8525\n",
      "Epoch 73/150\n",
      "2255/2255 [==============================] - 1999s 886ms/step - loss: 0.0879 - iou_bk: 0.9690 - iou_txt: 0.8567 - val_loss: 0.0905 - val_iou_bk: 0.9675 - val_iou_txt: 0.8499\n",
      "Epoch 74/150\n",
      "2255/2255 [==============================] - 2019s 895ms/step - loss: 0.0898 - iou_bk: 0.9686 - iou_txt: 0.8532 - val_loss: 0.0920 - val_iou_bk: 0.9670 - val_iou_txt: 0.8502\n",
      "Epoch 75/150\n",
      "2255/2255 [==============================] - 1995s 884ms/step - loss: 0.0879 - iou_bk: 0.9686 - iou_txt: 0.8568 - val_loss: 0.0890 - val_iou_bk: 0.9681 - val_iou_txt: 0.8541\n",
      "Epoch 76/150\n",
      "2255/2255 [==============================] - 2003s 888ms/step - loss: 0.0872 - iou_bk: 0.9693 - iou_txt: 0.8579 - val_loss: 0.0901 - val_iou_bk: 0.9680 - val_iou_txt: 0.8526\n",
      "Epoch 77/150\n",
      "2255/2255 [==============================] - 2001s 888ms/step - loss: 0.0875 - iou_bk: 0.9691 - iou_txt: 0.8570 - val_loss: 0.0895 - val_iou_bk: 0.9679 - val_iou_txt: 0.8529\n",
      "Epoch 78/150\n",
      "2255/2255 [==============================] - 1998s 886ms/step - loss: 0.0882 - iou_bk: 0.9692 - iou_txt: 0.8558 - val_loss: 0.0883 - val_iou_bk: 0.9686 - val_iou_txt: 0.8546\n",
      "Epoch 79/150\n",
      "2255/2255 [==============================] - 1994s 884ms/step - loss: 0.0874 - iou_bk: 0.9688 - iou_txt: 0.8571 - val_loss: 0.0874 - val_iou_bk: 0.9689 - val_iou_txt: 0.8564\n",
      "Epoch 80/150\n",
      "2255/2255 [==============================] - 2015s 893ms/step - loss: 0.0867 - iou_bk: 0.9695 - iou_txt: 0.8587 - val_loss: 0.0897 - val_iou_bk: 0.9678 - val_iou_txt: 0.8535\n",
      "Epoch 81/150\n",
      "2255/2255 [==============================] - 1991s 883ms/step - loss: 0.0862 - iou_bk: 0.9696 - iou_txt: 0.8595 - val_loss: 0.0898 - val_iou_bk: 0.9679 - val_iou_txt: 0.8543\n",
      "Epoch 82/150\n",
      "2255/2255 [==============================] - 2005s 889ms/step - loss: 0.0856 - iou_bk: 0.9697 - iou_txt: 0.8604 - val_loss: 0.0903 - val_iou_bk: 0.9673 - val_iou_txt: 0.8521\n",
      "Epoch 83/150\n",
      "   1/2255 [..............................] - ETA: 25:03 - loss: 0.0611 - iou_bk: 0.9668 - iou_txt: 0.9080"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Program Files\\Anaconda3\\lib\\site-packages\\keras\\callbacks.py:122: UserWarning: Method on_batch_end() is slow compared to the batch update (0.838847). Check your callbacks.\n",
      "  % delta_t_median)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2255/2255 [==============================] - 1997s 886ms/step - loss: 0.0885 - iou_bk: 0.9684 - iou_txt: 0.8555 - val_loss: 0.0876 - val_iou_bk: 0.9687 - val_iou_txt: 0.8558\n",
      "Epoch 84/150\n",
      "2255/2255 [==============================] - 2003s 888ms/step - loss: 0.0874 - iou_bk: 0.9696 - iou_txt: 0.8574 - val_loss: 0.0864 - val_iou_bk: 0.9694 - val_iou_txt: 0.8579\n",
      "Epoch 85/150\n",
      "2255/2255 [==============================] - 1991s 883ms/step - loss: 0.0850 - iou_bk: 0.9705 - iou_txt: 0.8616 - val_loss: 0.0856 - val_iou_bk: 0.9697 - val_iou_txt: 0.8587\n",
      "Epoch 86/150\n",
      "2255/2255 [==============================] - 1990s 883ms/step - loss: 0.0861 - iou_bk: 0.9694 - iou_txt: 0.8593 - val_loss: 0.0872 - val_iou_bk: 0.9683 - val_iou_txt: 0.8557\n",
      "Epoch 87/150\n",
      "2255/2255 [==============================] - 2021s 896ms/step - loss: 0.0861 - iou_bk: 0.9694 - iou_txt: 0.8597 - val_loss: 0.0903 - val_iou_bk: 0.9679 - val_iou_txt: 0.8536\n",
      "Epoch 88/150\n",
      "2255/2255 [==============================] - 2001s 887ms/step - loss: 0.0857 - iou_bk: 0.9701 - iou_txt: 0.8603 - val_loss: 0.0872 - val_iou_bk: 0.9686 - val_iou_txt: 0.8548\n",
      "Epoch 89/150\n",
      "2255/2255 [==============================] - 1993s 884ms/step - loss: 0.0858 - iou_bk: 0.9695 - iou_txt: 0.8600 - val_loss: 0.0871 - val_iou_bk: 0.9693 - val_iou_txt: 0.8564\n",
      "Epoch 90/150\n",
      "2255/2255 [==============================] - 1985s 880ms/step - loss: 0.0859 - iou_bk: 0.9698 - iou_txt: 0.8603 - val_loss: 0.0878 - val_iou_bk: 0.9685 - val_iou_txt: 0.8550\n",
      "Epoch 91/150\n",
      "2255/2255 [==============================] - 2021s 896ms/step - loss: 0.0849 - iou_bk: 0.9704 - iou_txt: 0.8614 - val_loss: 0.0912 - val_iou_bk: 0.9670 - val_iou_txt: 0.8488\n",
      "Epoch 92/150\n",
      "2255/2255 [==============================] - 1993s 884ms/step - loss: 0.0848 - iou_bk: 0.9703 - iou_txt: 0.8615 - val_loss: 0.0900 - val_iou_bk: 0.9675 - val_iou_txt: 0.8519\n",
      "Epoch 93/150\n",
      "   2/2255 [..............................] - ETA: 26:02 - loss: 0.0738 - iou_bk: 0.9758 - iou_txt: 0.8750"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Program Files\\Anaconda3\\lib\\site-packages\\keras\\callbacks.py:122: UserWarning: Method on_batch_end() is slow compared to the batch update (0.987048). Check your callbacks.\n",
      "  % delta_t_median)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2255/2255 [==============================] - 2011s 892ms/step - loss: 0.0853 - iou_bk: 0.9699 - iou_txt: 0.8606 - val_loss: 0.0872 - val_iou_bk: 0.9693 - val_iou_txt: 0.8554\n",
      "Epoch 94/150\n",
      "2255/2255 [==============================] - 1987s 881ms/step - loss: 0.0836 - iou_bk: 0.9710 - iou_txt: 0.8640 - val_loss: 0.0851 - val_iou_bk: 0.9697 - val_iou_txt: 0.8589\n",
      "Epoch 95/150\n",
      "2255/2255 [==============================] - 2002s 888ms/step - loss: 0.0846 - iou_bk: 0.9702 - iou_txt: 0.8622 - val_loss: 0.0860 - val_iou_bk: 0.9692 - val_iou_txt: 0.8586\n",
      "Epoch 96/150\n",
      "2255/2255 [==============================] - 2007s 890ms/step - loss: 0.0857 - iou_bk: 0.9697 - iou_txt: 0.8602 - val_loss: 0.0937 - val_iou_bk: 0.9668 - val_iou_txt: 0.8511\n",
      "Epoch 97/150\n",
      "2255/2255 [==============================] - 1998s 886ms/step - loss: 0.0844 - iou_bk: 0.9705 - iou_txt: 0.8624 - val_loss: 0.0866 - val_iou_bk: 0.9694 - val_iou_txt: 0.8587\n",
      "Epoch 98/150\n",
      "2255/2255 [==============================] - 2003s 888ms/step - loss: 0.0842 - iou_bk: 0.9702 - iou_txt: 0.8626 - val_loss: 0.0888 - val_iou_bk: 0.9682 - val_iou_txt: 0.8542\n",
      "Epoch 99/150\n",
      "2255/2255 [==============================] - 1990s 883ms/step - loss: 0.0840 - iou_bk: 0.9703 - iou_txt: 0.8632 - val_loss: 0.0866 - val_iou_bk: 0.9691 - val_iou_txt: 0.8577\n",
      "Epoch 100/150\n",
      "2255/2255 [==============================] - 1981s 879ms/step - loss: 0.0841 - iou_bk: 0.9706 - iou_txt: 0.8633 - val_loss: 0.0874 - val_iou_bk: 0.9693 - val_iou_txt: 0.8567\n",
      "Epoch 101/150\n",
      "2255/2255 [==============================] - 2021s 896ms/step - loss: 0.0845 - iou_bk: 0.9701 - iou_txt: 0.8622 - val_loss: 0.0890 - val_iou_bk: 0.9682 - val_iou_txt: 0.8552\n",
      "Epoch 102/150\n",
      "2255/2255 [==============================] - 1988s 882ms/step - loss: 0.0840 - iou_bk: 0.9706 - iou_txt: 0.8633 - val_loss: 0.0849 - val_iou_bk: 0.9699 - val_iou_txt: 0.8604\n",
      "Epoch 103/150\n",
      "2255/2255 [==============================] - 1999s 886ms/step - loss: 0.0835 - iou_bk: 0.9707 - iou_txt: 0.8639 - val_loss: 0.0886 - val_iou_bk: 0.9673 - val_iou_txt: 0.8520\n",
      "Epoch 104/150\n",
      "2255/2255 [==============================] - 2006s 890ms/step - loss: 0.0846 - iou_bk: 0.9701 - iou_txt: 0.8620 - val_loss: 0.0850 - val_iou_bk: 0.9696 - val_iou_txt: 0.8597\n",
      "Epoch 105/150\n",
      "2255/2255 [==============================] - 1999s 886ms/step - loss: 0.0828 - iou_bk: 0.9706 - iou_txt: 0.8652 - val_loss: 0.0876 - val_iou_bk: 0.9687 - val_iou_txt: 0.8563\n",
      "Epoch 106/150\n",
      "2255/2255 [==============================] - 2010s 891ms/step - loss: 0.0828 - iou_bk: 0.9711 - iou_txt: 0.8654 - val_loss: 0.0859 - val_iou_bk: 0.9698 - val_iou_txt: 0.8593\n",
      "Epoch 107/150\n",
      "2255/2255 [==============================] - 1993s 884ms/step - loss: 0.0823 - iou_bk: 0.9710 - iou_txt: 0.8660 - val_loss: 0.0876 - val_iou_bk: 0.9684 - val_iou_txt: 0.8554\n",
      "Epoch 108/150\n",
      "2255/2255 [==============================] - 1988s 882ms/step - loss: 0.0829 - iou_bk: 0.9710 - iou_txt: 0.8652 - val_loss: 0.0871 - val_iou_bk: 0.9690 - val_iou_txt: 0.8565\n",
      "Epoch 109/150\n",
      "2255/2255 [==============================] - 2026s 899ms/step - loss: 0.0834 - iou_bk: 0.9702 - iou_txt: 0.8641 - val_loss: 0.0873 - val_iou_bk: 0.9687 - val_iou_txt: 0.8543\n",
      "Epoch 110/150\n",
      "1327/2255 [================>.............] - ETA: 12:35 - loss: 0.0845 - iou_bk: 0.9705 - iou_txt: 0.8625"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-19-a100095263ec>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      9\u001b[0m                           \u001b[0mworkers\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m     10\u001b[0m                           \u001b[0mmax_queue_size\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m16\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m---> 11\u001b[0;31m                           callbacks=[checkpoint,tb])\n\u001b[0m",
      "\u001b[0;32mC:\\Program Files\\Anaconda3\\lib\\site-packages\\keras\\legacy\\interfaces.py\u001b[0m in \u001b[0;36mwrapper\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m     89\u001b[0m                 warnings.warn('Update your `' + object_name + '` call to the ' +\n\u001b[1;32m     90\u001b[0m                               'Keras 2 API: ' + signature, stacklevel=2)\n\u001b[0;32m---> 91\u001b[0;31m             \u001b[1;32mreturn\u001b[0m \u001b[0mfunc\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     92\u001b[0m         \u001b[0mwrapper\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_original_function\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mfunc\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m     93\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0mwrapper\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mC:\\Program Files\\Anaconda3\\lib\\site-packages\\keras\\engine\\training.py\u001b[0m in \u001b[0;36mfit_generator\u001b[0;34m(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)\u001b[0m\n\u001b[1;32m   1416\u001b[0m             \u001b[0muse_multiprocessing\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0muse_multiprocessing\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m   1417\u001b[0m             \u001b[0mshuffle\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mshuffle\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1418\u001b[0;31m             initial_epoch=initial_epoch)\n\u001b[0m\u001b[1;32m   1419\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m   1420\u001b[0m     \u001b[1;33m@\u001b[0m\u001b[0minterfaces\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlegacy_generator_methods_support\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mC:\\Program Files\\Anaconda3\\lib\\site-packages\\keras\\engine\\training_generator.py\u001b[0m in \u001b[0;36mfit_generator\u001b[0;34m(model, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)\u001b[0m\n\u001b[1;32m    179\u001b[0m             \u001b[0mbatch_index\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    180\u001b[0m             \u001b[1;32mwhile\u001b[0m \u001b[0msteps_done\u001b[0m \u001b[1;33m<\u001b[0m \u001b[0msteps_per_epoch\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m--> 181\u001b[0;31m                 \u001b[0mgenerator_output\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnext\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0moutput_generator\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    182\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    183\u001b[0m                 \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mhasattr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mgenerator_output\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'__len__'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mC:\\Program Files\\Anaconda3\\lib\\site-packages\\keras\\utils\\data_utils.py\u001b[0m in \u001b[0;36mget\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m    683\u001b[0m         \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    684\u001b[0m             \u001b[1;32mwhile\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mis_running\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m--> 685\u001b[0;31m                 \u001b[0minputs\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mqueue\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mblock\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mTrue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    686\u001b[0m                 \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mqueue\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtask_done\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    687\u001b[0m                 \u001b[1;32mif\u001b[0m \u001b[0minputs\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mC:\\Program Files\\Anaconda3\\lib\\multiprocessing\\pool.py\u001b[0m in \u001b[0;36mget\u001b[0;34m(self, timeout)\u001b[0m\n\u001b[1;32m    600\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    601\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mget\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtimeout\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m--> 602\u001b[0;31m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwait\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtimeout\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    603\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mready\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    604\u001b[0m             \u001b[1;32mraise\u001b[0m \u001b[0mTimeoutError\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mC:\\Program Files\\Anaconda3\\lib\\multiprocessing\\pool.py\u001b[0m in \u001b[0;36mwait\u001b[0;34m(self, timeout)\u001b[0m\n\u001b[1;32m    597\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    598\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mwait\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtimeout\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m--> 599\u001b[0;31m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_event\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwait\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtimeout\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    600\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    601\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mget\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtimeout\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mC:\\Program Files\\Anaconda3\\lib\\threading.py\u001b[0m in \u001b[0;36mwait\u001b[0;34m(self, timeout)\u001b[0m\n\u001b[1;32m    547\u001b[0m             \u001b[0msignaled\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_flag\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    548\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0msignaled\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m--> 549\u001b[0;31m                 \u001b[0msignaled\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_cond\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwait\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtimeout\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    550\u001b[0m             \u001b[1;32mreturn\u001b[0m \u001b[0msignaled\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    551\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mC:\\Program Files\\Anaconda3\\lib\\threading.py\u001b[0m in \u001b[0;36mwait\u001b[0;34m(self, timeout)\u001b[0m\n\u001b[1;32m    291\u001b[0m         \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m    \u001b[1;31m# restore state no matter what (e.g., KeyboardInterrupt)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    292\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0mtimeout\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m--> 293\u001b[0;31m                 \u001b[0mwaiter\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0macquire\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    294\u001b[0m                 \u001b[0mgotit\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m    295\u001b[0m             \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "parallel_model.optimizer.lr = 1e-6\n",
    "res = parallel_model.fit_generator(gen_train,\n",
    "                          steps_per_epoch =gen_train.num_samples()// batch_size,\n",
    "                          epochs = 150,\n",
    "                          validation_data=gen_test,\n",
    "                          validation_steps =gen_test.num_samples()//batch_size,\n",
    "                          verbose=1,\n",
    "                          initial_epoch=70,\n",
    "                          workers=4,\n",
    "                          max_queue_size=16,\n",
    "                          callbacks=[checkpoint,tb])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "#model.load_weights('resent50-29.hdf5')\n",
    "parallel_model.load_weights('resent50-190219_BLINEAR-102.hdf5')\n",
    "model.save_weights('resent50-190219_BLINEAR-iou8604.hdf5')\n",
    "model.load_weights('resent50-190219_BLINEAR-iou8604.hdf5')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.load_weights('./tf/resnet50.hdf5')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'gen_test' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-15-b6ab4c190477>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mimages\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mlables\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnext\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mgen_test\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m: name 'gen_test' is not defined"
     ]
    }
   ],
   "source": [
    "images,lables = next(gen_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import glob\n",
    "import cv2\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np \n",
    "import os\n",
    "import tqdm\n",
    "#dir = r'C:\\jianweidata\\ocr\\psenet\\Extraction Test\\2x'\n",
    "dir = r'E:\\psenet-MTWI\\document\\mtwi_2018_task2_test\\icpr_mtwi_task2\\image_test'\n",
    "imagesfile = glob.glob(os.path.join(dir,'*.jpg'))\n",
    "MIN_LEN = 640\n",
    "MAX_LEN = 1024"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|████████████████████████████████████████████████████████████████████████████| 10000/10000 [32:16<00:00,  4.85it/s]\n"
     ]
    }
   ],
   "source": [
    "from tool.utils import ufunc_4 , scale_expand_kernels ,fit_minarearectange,fit_boundingRect,save_MTWI_2108_resault\n",
    "with tqdm.tqdm(total = len(imagesfile)) as bar:\n",
    "    for i,j in enumerate(imagesfile):\n",
    "                bar.update()\n",
    "                #j = r'E:\\psenet-MTWI\\document\\mtwi_2018_task2_test\\icpr_mtwi_task2\\image_test\\T1..67FbxfXXXXXXXX_!!0-item_pic.jpg.jpg'\n",
    "                images=cv2.imdecode(np.fromfile(j,dtype=np.uint8),-1) \n",
    "                #images=cv2.cvtColor(images,cv2.COLOR_RGB2BGR)\n",
    "                \n",
    "                h,w = images.shape[0:2]\n",
    "#                 if(h>w and h>MAX_LEN):\n",
    "#                     w = MAX_LEN / h * w   \n",
    "#                     h = MAX_LEN\n",
    "#                 elif(w>=h and w>MAX_LEN):\n",
    "#                     h = MAX_LEN / w * h   \n",
    "#                     w = MAX_LEN\n",
    "\n",
    "                if(w<h and w<MIN_LEN):\n",
    "                    h = MIN_LEN / w * h               \n",
    "                    w = MIN_LEN\n",
    "                elif(h<=w and h<MIN_LEN):\n",
    "                    w = MIN_LEN / h * w         \n",
    "                    h = MIN_LEN\n",
    "                    \n",
    "                w = min(w,MAX_LEN)    \n",
    "                h = min(h,MAX_LEN)\n",
    "                \n",
    "                \n",
    "                w = int(w //32 * 32)\n",
    "                h = int(h//32 * 32)\n",
    "                \n",
    "#                 w = 640\n",
    "#                 h = 640 \n",
    "                scalex = images.shape[1] / w\n",
    "                scaley = images.shape[0] / h\n",
    "                \n",
    "                images = cv2.resize(images,(w,h),cv2.INTER_AREA)\n",
    "                images = np.reshape(images,(1,h,w,3))            \n",
    "                \n",
    "                res = model.predict(images)\n",
    "                res1 = res[0]\n",
    "                res1[res1>0.9]= 1\n",
    "                res1[res1<=0.9]= 0\n",
    "                newres1 = []\n",
    "                for i in range(5):\n",
    "                    n = np.logical_and(res1[:,:,5],res1[:,:,i]) * 255\n",
    "                    newres1.append(n)\n",
    "                newres1.append(res1[:,:,5]*255)\n",
    "                num_label,labelimage = scale_expand_kernels(newres1)\n",
    "                rects = fit_minarearectange(num_label,labelimage)\n",
    "\n",
    "                \n",
    "                cv2.drawContours(images[0],np.array(rects)*2,-1,(0,0,255),2)\n",
    "                \n",
    "                base_name = '.'.join(os.path.basename(j).split('.')[:-1])\n",
    "                cv2.imwrite(os.path.join(dir,base_name+'_6.tif'),images[0])\n",
    "                \n",
    "                save_MTWI_2108_resault(os.path.join(dir,base_name+'.txt'),np.array(rects)*2,scalex ,scaley)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
